package com.xrz.sxm.aj.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xrz.sxm.aj.base.BaseAct;
import com.xrz.sxm.aj.entity.RegBean;
import com.xrz.sxm.aj.https.Http;
import com.xrz.sxm.aj.https.HttpConst;
import com.xrz.sxm.aj.https.HttpListener;
import com.xrz.sxm.aj.utils.Utils;

import java.util.HashMap;
import java.util.Map;

public class Act_Reg extends BaseAct implements RadioGroup.OnCheckedChangeListener {
    @ViewInject(R.id.reg_username)
    private EditText m_username_EditText;
    @ViewInject(R.id.reg_number)
    private EditText m_num_EditText;
    @ViewInject(R.id.reg_age)
    private EditText m_age_EditText;
    @ViewInject(R.id.reg_address)
    private EditText m_address_EditText;
    @ViewInject(R.id.reg_password)
    private EditText m_password_EditText;
    @ViewInject(R.id.reg_repassword)
    private EditText m_repassword_EditText;
    @ViewInject(R.id.reg_rgp)
    private RadioGroup RGP;
    @ViewInject(R.id.rbt1)
    private RadioButton Rbt1;
    @ViewInject(R.id.rbt2)
    private RadioButton Rbt2;
    private String sex = "男";

    @Override
    protected String setTopTitle() {
        return "注册";
    }

    @Override
    protected int setView() {
        return R.layout.act__reg;
    }

    @Override
    protected void init() {
        RGP.setOnCheckedChangeListener(this);
        //获取地址
        Http.getInstant().sendRequest(getApplicationContext(), false, HttpConst.URL_ADRESS, null, new HttpListener() {
            @Override
            public void onHttpSuccess(String result) {
                JSONObject object = JSON.parseObject(result);
                int code = object.getInteger("code");
                String content = object.getString("data");
                if (code == 0) {
                    RegBean bean = JSON.parseObject(content, RegBean.class);
                    m_address_EditText.setText(bean.getRegion() + " " + bean.getCity());
                }
            }

            @Override
            public void onHttpError(String error) {
                Toast.makeText(Act_Reg.this, error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void onRegClick(View view) {
        String username = m_username_EditText.getText().toString().trim();
        String num = m_num_EditText.getText().toString().trim();
        String password = m_password_EditText.getText().toString().trim();
        String repassword = m_repassword_EditText.getText().toString().trim();
        String age = m_age_EditText.getText().toString().trim();
        String address = m_address_EditText.getText().toString().trim();
        if (username.equals("") || num.equals("") || TextUtils.isEmpty(password)) {
            Toast.makeText(getApplicationContext(), "用户名，手机号，密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!Utils.isMobileNO(num)) {
            Toast.makeText(getApplicationContext(), "手机号码格式不对!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!password.equals(repassword)) {
            Toast.makeText(getApplicationContext(), "两次密码不一致!", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("dh", num);
        map.put("dlmm", password);
        map.put("dz", address);
        map.put("nl", age);
        map.put("xb", sex);
        map.put("xm", username);
        Http.getInstant().sendRequest(getApplicationContext(), true, HttpConst.URL_REG, map, new HttpListener() {
            @Override
            public void onHttpSuccess(String result) {
                JSONObject object = JSON.parseObject(result);
                int status = object.getIntValue("status");
                String msg = object.getString("msg");
                if (status == -1) {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onHttpError(String error) {
                showMessageDialog("提示", "注册失败，请稍候再试");
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (radioGroup.getId()) {
            case R.id.rbt1:
                sex = Rbt1.getText().toString();
                break;
            case R.id.rbt2:
                sex = Rbt2.getText().toString();
                break;
        }
    }
}
